package com.jmxcfc.blfsc.huisheng.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jmxcfc.blfsc.common.common.ComConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Objects;

/**
 * http请求工具类
 */
@Slf4j
@Component
public class HttpClientUtil {

    private RestTemplate restTemplate;

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 生成map
     * @param params 多个参数，key，value组合
     * @return map集合
     */
    public static Map<String, String> createMap(String... params) {
        if (params.length % 2 != 0) {
            throw new RuntimeException("请保持key，value一一配对");
        }
        Map<String, String> mapParams = Maps.newHashMapWithExpectedSize(params.length / 2);
        for (int i = 0; i < params.length; i += 2) {
            mapParams.put(params[i], params[i + 1]);
        }
        return mapParams;
    }

    /**
     * 带请求头的GET请求调用方式
     * @param url          请求URL
     * @param headers      请求头参数
     * @param responseType 返回对象类型
     * @param uriVariables URL中的变量，与Map中的key对应
     * @return ResponseEntity 响应对象封装类
     */
    public <T> ResponseEntity<T> get(String url, String body, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
        return exchange(url, HttpMethod.GET, body, headers, responseType, uriVariables);
    }


    /**
     * 自定义请求头和请求体的POST请求调用方式
     * @param url
     * @param body
     * @param headers
     * @param responseType
     * @return
     * @param <T>
     */
    public <T> ResponseEntity<T> post(String url, String body, Map<String, String> headers, Class<T> responseType) {
        return exchange(url, HttpMethod.POST, body, headers, responseType);
    }


    /**
     * 通用调用方式
     * @param url          请求URL
     * @param method       请求方法类型
     * @param body         JSONObject.toStrig()
     * @param headers      请求头参数
     * @param responseType 返回对象类型
     * @param uriVariables URL中的变量，与Map中的key对应
     * @return ResponseEntity 响应对象封装类
     */
    private <T> ResponseEntity<T> exchange(String url, HttpMethod method, String body, Map<String, String> headers, Class<T> responseType, Map<String, ?> uriVariables) {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.parseMediaType(ComConstants.MEDIA_TYPE));
        requestHeaders.setAll(headers);
        //设置访问的Entity
        HttpEntity<String> requestEntity = new HttpEntity<>(body, requestHeaders);
        return restTemplate.exchange(url, method, requestEntity, responseType, uriVariables);
    }

    /**
     * 通用调用方式
     *
     * @param url          请求URL
     * @param method       请求方法类型
     * @param body         JSONObject.toStrig()
     * @param headers      请求头参数
     * @param responseType 返回对象类型
     * @return ResponseEntity 响应对象封装类
     */
    private <T> ResponseEntity<T> exchange(String url, HttpMethod method, String body, Map<String, String> headers, Class<T> responseType) {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.parseMediaType(ComConstants.MEDIA_TYPE));
        requestHeaders.setAll(headers);
        //设置访问的Entity
        HttpEntity<String> requestEntity = new HttpEntity<>(body,requestHeaders);
        return restTemplate.exchange(url, method, requestEntity, responseType);
    }

}
